package cn.com.oceansoft.osc.ms.utils;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.XmlFriendlyReplacer;
import com.thoughtworks.xstream.io.xml.XppDriver;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import sun.misc.BASE64Encoder;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wuzy
 * @created 2017-03-24-10:08
 * @email wzy@oceansoft.com.cn
 */
public class ExpressUtil {
    private String sFWebServiceCheckWord;

    private String sFWebServiceUrl;

    public String getsFWebServiceCheckWord() {
        return sFWebServiceCheckWord;
    }

    public void setsFWebServiceCheckWord(String sFWebServiceCheckWord) {
        this.sFWebServiceCheckWord = sFWebServiceCheckWord;
    }

    public String getsFWebServiceUrl() {
        return sFWebServiceUrl;
    }

    public void setsFWebServiceUrl(String sFWebServiceUrl) {
        this.sFWebServiceUrl = sFWebServiceUrl;
    }


    public String md5EncryptAndBase64(String str) {
        return encodeBase64(md5Encrypt(str));
    }

    private byte[] md5Encrypt(String encryptStr) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(encryptStr.getBytes("utf8"));
            return md5.digest();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private String encodeBase64(byte[] b) {
        sun.misc.BASE64Encoder base64Encode = new BASE64Encoder();
        String str = base64Encode.encode(b);
        return str;
    }

    public String GetResponseFromService(String reqxml) throws Exception {

        String verifyCode = md5EncryptAndBase64(reqxml + sFWebServiceCheckWord);
        Map<String, String> map = new HashMap<>();
        map.put("xml", reqxml);
        map.put("verifyCode", verifyCode);


        String response = HttpClientUtil.doPostByMap(sFWebServiceUrl, map, "UTF-8");
        return response;

    }


    public String GetValueByXpathAttbute(String xmlString, String xpath) {
        SAXReader saxReader = new SAXReader();
        String returnValue = "";

        try {
            InputStream is = new ByteArrayInputStream(xmlString.getBytes("UTF-8"));
            Document document;
            document = saxReader.read(is);
            Attribute node = (Attribute) document.selectSingleNode(xpath);
            if (null == node) {

                return returnValue;
            } else {
                returnValue = node.getText();

            }
            return returnValue;
        } catch (Exception e) {
            e.printStackTrace();
            return returnValue;
        }
    }

    public String GetValueByXpathElement(String xmlString, String xpath) {
        SAXReader saxReader = new SAXReader();
        String returnValue = "";

        try {
            InputStream is = new ByteArrayInputStream(xmlString.getBytes("UTF-8"));
            Document document;
            document = saxReader.read(is);
            Element node = (Element) document.selectSingleNode(xpath);
            if (null == node) {

                return returnValue;
            } else {
                returnValue = node.getText();

            }
            return returnValue;
        } catch (Exception e) {
            e.printStackTrace();
            return returnValue;
        }
    }

    public String GetValueByXpathElementXml(String xmlString, String xpath) {
        SAXReader saxReader = new SAXReader();
        String returnValue = "";

        try {
            InputStream is = new ByteArrayInputStream(xmlString.getBytes("UTF-8"));
            Document document;
            document = saxReader.read(is);
            Element node = (Element) document.selectSingleNode(xpath);
            if (null == node) {

                return returnValue;
            } else {
                returnValue = node.asXML();

            }
            return returnValue;
        } catch (Exception e) {
            e.printStackTrace();
            return returnValue;
        }
    }

    public <T> T toBean(String xmlStr, Class<T> cls) {
        try {


            //XStream xstream = new XStream(new DomDriver());
            XStream xstream = new XStream(new XppDriver(new
                    XmlFriendlyReplacer("_-", "_")));
            xstream.processAnnotations(cls);
            @SuppressWarnings("unchecked")
            InputStream is = new ByteArrayInputStream(xmlStr.getBytes("UTF-8"));
            T t = (T) xstream.fromXML(is);
            return t;
        }catch (Exception e)
        {
            e.printStackTrace();
            return  null;
        }

    }
}
